package edu.ybu.manage.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ybu.manage.pojo.Dealer;
import edu.ybu.manage.service.DealerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dealer")
public class DealerController {
    @Autowired
    private DealerService dealerService;

    @RequestMapping("/selectAllByPage")
    public Map<String,Object> selectAllByPage(Integer page,Integer limit){
        List<Dealer> dealers = dealerService.selectAllByPage(page-1, limit);
        Integer count = dealerService.selectAllCount();
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "查询成功");
        res.put("count", count);
        res.put("data", dealers);
        return res;
    }
    @RequestMapping("/insertDealer")
    public Integer insertDealer(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Dealer dealer = null;
        try {
            dealer = objectMapper.readValue(json, Dealer.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (dealer==null){
            return 0;
        }
        return dealerService.insertDealer(dealer);
    }
    @RequestMapping("/deleteById")
    public Integer deleteById(Integer id){
        return dealerService.deleteById(id);
    }
    @RequestMapping("/deleteByIds")
    public Integer deleteByIds(String json){
        System.out.println(json);
        ObjectMapper objectMapper = new ObjectMapper();
        Dealer[] dealers = new Dealer[0];
        try {
            dealers = objectMapper.readValue(json, Dealer[].class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        ArrayList<Integer> integers = new ArrayList<>();
        for (Dealer dealer : dealers) {
            Integer id = dealer.getId();
            integers.add(id);
        }
        return dealerService.deleteByIds(integers);
    }
    @RequestMapping("/updateById")
    public Integer updateById(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Dealer dealer = null;
        try {
            dealer = objectMapper.readValue(json, Dealer.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        return dealerService.updateById(dealer);
    }

    @RequestMapping("/selectByCondition")
    public Map<String,Object> selectByCondition(Integer page, Integer limit, String dealername,
                                                String dealerdesc,String address, Integer status){
        Dealer dealer = new Dealer();
        dealer.setDealername(dealername);
        dealer.setDealerdesc(dealerdesc);
        dealer.setAddress(address);
        dealer.setStatus(status);
        System.out.println(dealer);
        List<Dealer> dealers = dealerService.selectByCondition(dealer);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", dealers.size());
        res.put("data", dealers);
        return  res;
    }
}
